<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.kilowattmeter_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/meter/kilowattmeter";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "计量点编号",
              prop: "kmNo",
              rules: [{
                required: true,
                message: "请输入计量点编号",
                trigger: "blur"
              }]
            },
            {
              label: "计量点名称",
              prop: "kmName",
              rules: [{
                required: true,
                message: "请输入计量点名称",
                trigger: "blur"
              }]
            },
            {
              label: "缴费优先级",
              prop: "payPriority",
              rules: [{
                required: true,
                message: "请输入缴费优先级",
                trigger: "blur"
              }]
            },
            {
              label: "抄表方式",
              prop: "recordMode",
              rules: [{
                required: true,
                message: "请输入抄表方式",
                trigger: "blur"
              }]
            },
            {
              label: "计量点状态",
              prop: "kmStatus",
              rules: [{
                required: true,
                message: "请输入计量点状态",
                trigger: "blur"
              }]
            },
            {
              label: "最新读数",
              prop: "readValue",
              rules: [{
                required: true,
                message: "请输入最新读数",
                trigger: "blur"
              }]
            },
            {
              label: "读数时间",
              prop: "readTime",
              rules: [{
                required: true,
                message: "请输入读数时间",
                trigger: "blur"
              }]
            },
            {
              label: "上次读数",
              prop: "preReadValue",
              rules: [{
                required: true,
                message: "请输入上次读数",
                trigger: "blur"
              }]
            },
            {
              label: "上次读数时间",
              prop: "preReadTime",
              rules: [{
                required: true,
                message: "请输入上次读数时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：尖值",
              prop: "sharpReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：尖值",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：尖值读取时间",
              prop: "sharpReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：尖值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次尖值",
              prop: "preSharpReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次尖值",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次尖值读取时间",
              prop: "preSharpReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次尖值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：峰",
              prop: "peekReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：峰",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：峰值读取时间",
              prop: "peekReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：峰值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次峰值",
              prop: "prePeekReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次峰值",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次峰值读取时间",
              prop: "prePeekReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次峰值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：平",
              prop: "flatReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：平",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：平值读取时间",
              prop: "flatReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：平值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次平值",
              prop: "preFlatReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次平值",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次平值读取时间",
              prop: "preFlatReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次平值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：谷",
              prop: "lowReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：谷",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：谷值读取时间",
              prop: "lowReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：谷值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次谷值",
              prop: "preLowReadValue",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次谷值",
                trigger: "blur"
              }]
            },
            {
              label: "分时段计费：上次谷值读取时间",
              prop: "preLowReadTime",
              rules: [{
                required: true,
                message: "请输入分时段计费：上次谷值读取时间",
                trigger: "blur"
              }]
            },
            {
              label: "用电类型：工业用电、商业用电、家庭用电",
              prop: "voltageType",
              rules: [{
                required: true,
                message: "请输入用电类型：工业用电、商业用电、家庭用电",
                trigger: "blur"
              }]
            },
            {
              label: "电价计算方式：普通计费；阶梯计费；分时段计费",
              prop: "priceType",
              rules: [{
                required: true,
                message: "请输入电价计算方式：普通计费；阶梯计费；分时段计费",
                trigger: "blur"
              }]
            },
            {
              label: "功率因数标准",
              prop: "powerFactor",
              rules: [{
                required: true,
                message: "请输入功率因数标准",
                trigger: "blur"
              }]
            },
            {
              label: "功率因数考核方式",
              prop: "powerFactorAssess",
              rules: [{
                required: true,
                message: "请输入功率因数考核方式",
                trigger: "blur"
              }]
            },
            {
              label: "线损分摊方式，0表示通过网损百分比计算，1表示手工录入网损电量",
              prop: "lineLossShareType",
              rules: [{
                required: true,
                message: "请输入线损分摊方式，0表示通过网损百分比计算，1表示手工录入网损电量",
                trigger: "blur"
              }]
            },
            {
              label: "线损计算方式",
              prop: "lineLossCalculation",
              rules: [{
                required: true,
                message: "请输入线损计算方式",
                trigger: "blur"
              }]
            },
            {
              label: "线损分摊协议值，录入网损百分比，比如录入8，表示8%",
              prop: "lineLossShareValue",
              rules: [{
                required: true,
                message: "请输入线损分摊协议值，录入网损百分比，比如录入8，表示8%",
                trigger: "blur"
              }]
            },
            {
              label: "有功线损计算值",
              prop: "activePowerLineLoss",
              rules: [{
                required: true,
                message: "请输入有功线损计算值",
                trigger: "blur"
              }]
            },
            {
              label: "无功线损计算值",
              prop: "reactivePowerLineLoss",
              rules: [{
                required: true,
                message: "请输入无功线损计算值",
                trigger: "blur"
              }]
            },
            {
              label: "报装容量",
              prop: "instCapacity",
              rules: [{
                required: true,
                message: "请输入报装容量",
                trigger: "blur"
              }]
            },
            {
              label: "抄表次数",
              prop: "meCount",
              rules: [{
                required: true,
                message: "请输入抄表次数",
                trigger: "blur"
              }]
            },
            {
              label: "分摊总户数",
              prop: "shareCount",
              rules: [{
                required: true,
                message: "请输入分摊总户数",
                trigger: "blur"
              }]
            },
            {
              label: "分摊总面积",
              prop: "shareAcreage",
              rules: [{
                required: true,
                message: "请输入分摊总面积",
                trigger: "blur"
              }]
            },
            {
              label: "分摊总容量",
              prop: "shareCapacity",
              rules: [{
                required: true,
                message: "请输入分摊总容量",
                trigger: "blur"
              }]
            },
            {
              label: "电源容量",
              prop: "capacity",
              rules: [{
                required: true,
                message: "请输入电源容量",
                trigger: "blur"
              }]
            },
            {
              label: "电源类型",
              prop: "powerType",
              rules: [{
                required: true,
                message: "请输入电源类型",
                trigger: "blur"
              }]
            },
            {
              label: "电源链接方式",
              prop: "powerLinkMode",
              rules: [{
                required: true,
                message: "请输入电源链接方式",
                trigger: "blur"
              }]
            },
            {
              label: "电源切换方式",
              prop: "powerSwitchMode",
              rules: [{
                required: true,
                message: "请输入电源切换方式",
                trigger: "blur"
              }]
            },
            {
              label: "所属客户",
              prop: "customerId",
              rules: [{
                required: true,
                message: "请输入所属客户",
                trigger: "blur"
              }]
            },
            {
              label: "所属用户",
              prop: "memberId",
              rules: [{
                required: true,
                message: "请输入所属用户",
                trigger: "blur"
              }]
            },
            {
              label: "合同id",
              prop: "contractId",
              rules: [{
                required: true,
                message: "请输入合同id",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remarks",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.kilowattmeter_add, false),
          viewBtn: this.vaildData(this.permission.kilowattmeter_view, false),
          delBtn: this.vaildData(this.permission.kilowattmeter_delete, false),
          editBtn: this.vaildData(this.permission.kilowattmeter_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
